<?php

include_once('./config/config.php');
include_once('./config/check.php');

$id = empty($_GET['id']) ? '' : $_GET['id'];
//查询这个用户名是否存在，防止他验证后再次提交
$sql = "SELECT d.name as d_name,j.name as j_name FROM {$pre_}departments as d INNER JOIN {$pre_}jobs as j ON  d.id = j.depid WHERE j.id = $id ";
$admin = find($sql);
// var_dump($admin);
// exit;

// $d_sql1 = "SELECT depid FROM {$pre_}jobs WHERE `id` = $id";
// $d_admin = find($d_sql1);
// var_dump($d_admin);
// exit;

if (!empty($_POST)) {
    $username = empty($_POST['username']) ? '' : $_POST['username'];
    // 查询这个用户名是否存在，防止他验证后再次提交
    $sql = "SELECT * FROM {$pre_}jobs WHERE `name` = '$username'";
    $job = find($sql);
    if ($job) {
        ShowMsg('职位已存在，请重新输入');
        exit;
    }

    //部门监测
    $department = empty($_POST['department']) ? '' : $_POST['department'];
    if (empty($department)) {
        ShowMsg("部门不能为空!");
        exit;
    }
    $d_sql = "SELECT * FROM {$pre_}departments WHERE `name` = '$department'";
    $departments = find($d_sql);
    
    if($departments){
        ShowMsg("可以修改!");
    }else{
        ShowMsg("不存在该部门!!");
    }
 

    //组装数据
    $data = [
        'name' => $username,
        'depid' => $department,
    ];

    //插入到数据中
    $insert = update('jobs', $data, 'id=' . $id);

    if ($insert) {
        ShowMsg('修改成功', 'job.php');
        exit;
    } else {
        ShowMsg('修改失败');
        exit;
    }
}
?>



<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php'); ?>
</head>

<body>


    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">发布文章</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.html">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">

                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 职位列表</button>
                    <div class="btn-group">
                    </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane active in" id="home">
                            <form method="post" enctype="multipart/form-data">
                                <label>部门</label>
                                <input type="text" name="department" value="<?php echo $admin['d_name'] ?>" class="input-xxlarge">
                                <label>职位</label>
                                <input type="text" name="username" class="input-xxlarge" value="<?php echo $admin['j_name']; ?>">
                                <span id="notice" style="color:red;display:none;">职位已注册</span>
                                <label></label>
                                <input id="add" class="btn btn-primary" type="submit" value="提交" />
                            </form>
                        </div>
                    </div>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Delete Confirmation</h3>
                    </div>
                    <div class="modal-body">

                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                        <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>

            </div>
        </div>
    </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
    $("input[name='username']").change(function() {

        //用户名
        var username = $(this).val()


        //JS版本

        //创建异步对象
        var ajax = new XMLHttpRequest();


        //绑定请求状态  当请求被发送到服务器时，我们需要执行一些基于响应的任务
        ajax.onreadystatechange = function() {
            // console.log(ajax.readyState)
            //http的状态码
            // console.log(ajax.status)
            // if(ajax.readyState == 4)
            if (ajax.status == 200) {
                //获取到的是一个 json的字符串
                // console.log(ajax.responseText)

                //将获取到的json 变成js的数据类型
                var result = JSON.parse(ajax.responseText)

                //根据结果来判断
                if (result.success) {
                    //可以注册
                    $("#notice").css({
                        'display': 'inline-block',
                        'color': 'green'
                    })
                    $("#notice").html(result.msg)
                    $("#add").prop('disabled', false)
                } else {
                    //不可以注册
                    $("#notice").css({
                        'display': 'inline-block',
                        'color': 'red'
                    })
                    $("#notice").html(result.msg)
                    $("#add").prop('disabled', true)
                }
            }
        }


        //发起请求  false 同步  true 异步
        ajax.open('post', 'AdminAdd.php?action=check', true)

        //post的请求一定要设置 http头信息
        ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

        //发送请求，并传递数据
        ajax.send(`username=${username}`)
    })
</script>